package com.example.mybatis_book.service;

import com.example.mybatis_book.mapper.BookInfoMapper;
import com.example.mybatis_book.model.BookInfo;
import com.example.mybatis_book.enums.BookStatus;
import com.example.mybatis_book.model.PageRequest;
import com.example.mybatis_book.model.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class BookInfoService {

    @Autowired
    BookInfoMapper bookMapper;

//    public List<BookInfo> getBookList() {
//        List<BookInfo> bookInfoList =  bookMapper.getBookList();
//        for (BookInfo book : bookInfoList) {
//            if (book.getStatus() == 2) {
//                book.setStatusCN("不可借阅");
//            }else if(book.getStatus() == 1) {
//                book.setStatusCN("可借阅");
//            }else {
//                book.setStatusCN("无效");
//            }
//
//        }
//        return bookInfoList;
//    }

    public Integer addBook(BookInfo bookInfo) {
        log.info("接受参数 bookInfo：",bookInfo);
        return bookMapper.addBook(bookInfo);
    }

    public PageResponse<BookInfo> getBookListByPage(PageRequest pageRequest) {
        List<BookInfo> bookInfoList =  bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        for (BookInfo book : bookInfoList) {
            book.setStatusCN(BookStatus.getMsgBYCode(book.getStatus()).getMsg());

//            if (book.getStatus() == 2) {
//                book.setStatusCN("不可借阅");
//            }else if(book.getStatus() == 1) {
//                book.setStatusCN("可借阅");
//            }else {
//                book.setStatusCN("删除");
//            }

        }
        Integer total = bookMapper.count();
        return new PageResponse<>(bookInfoList,total,pageRequest);
    }

//    public Integer count() {
//        return bookMapper.count();
//    }

    public BookInfo queryBookById(Integer id) {
        BookInfo bookInfo =  bookMapper.queryBookById(id);
        bookInfo.setStatusCN(BookStatus.getMsgBYCode(bookInfo.getStatus()).getMsg());
        return bookInfo;
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBookById(Integer bookId) {
        BookInfo bookInfo =  new  BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer bathDeleteBook(List<Integer> ids) {
        return bookMapper.bathDeleteBook(ids);
    }
}